#include "listTable.h"

listTable_t *initList()
{
    listTable_t *list = (struct listTable_s *)malloc(sizeof(listTable_t));
    list->count = 0;
    list->head = (listNode_t *)malloc(sizeof(listNode_t));
    memset(list->head, 0, sizeof(listNode_t));
    return list;
}

void destroyList(listTable_t *list)
{
    if (list != NULL)
    {
        if (list->head->next = NULL)
        {
            listNode_t *node = list->head;
            list->head = node->next;
            node->next = NULL;
            free(node);
        }
        free(list->head);
        free(list);
    }
}
int addListNode(listTable_t *list, void *value)
{
    listNode_t *node = (listNode_t *)malloc(sizeof(listNode_t));
    node->value = value;
    node->next = list->head->next;
    list->head->next = node;
    list->count++;
    return 0;
}

int delListNode(listTable_t *list, void *value)
{
    if (list->count == 0)
    {
        printf("链表为空\n");
        return -1;
    }
    listNode_t *curNode = list->head;

    while (curNode)
    {
        if (curNode->next->value == value)
        {
            listNode_t *node = curNode->next;
            curNode->next = node->next;
            node->next = NULL;
            free(node);
            list->count--;
            break;
        }
        curNode = curNode->next;
    }
    if (curNode == NULL)
    {
        printf("未找到该节点\n");
        return -1;
    }
    printf("删除成功\n");
    return 0;
}
